import { defineStore } from 'pinia';
import { SystemRoleInfoVO } from '@/entiy/vo/SystemRoleInfoVO.ts';
import RequestService from '@/service/RequestService.ts';
import PromiseUtils from '@/utils/PromiseUtils.ts';
import { SystemRoleAddFormData } from "@/entiy/form/SystemRoleAddFormData.ts";
import { BizError } from "@/common/errors/BizError.ts";

export const useSystemRoleManagerStore = defineStore('UseSystemRoleManagerStore', {
    state: () => {
        return {
            // 页面加载中
            isPageLoading: false,

            // 表格加载中
            isTableLoading: false,

            // 表格数据
            tableDataList: [] as Array<SystemRoleInfoVO>,

            // 添加系统菜单弹窗展示状态
            isShowAddSystemMenuModel: false,
        };
    },

    getters: {},

    actions: {
        // 获取列表数据
        async getTableDataList() {
            const result = await RequestService.get<Array<SystemRoleInfoVO>>('get-all-system-role-info-list');
            if (result.isError) {
                throw new BizError(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        // 添加系统角色
        async addSystemRole(params: SystemRoleAddFormData) {
            const result = await RequestService.post<Array<SystemRoleInfoVO>>('add-system-role-info', params);
            if (result.isError) {
                throw new BizError(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        // 删除系统角色
        async deleteSystemRole(row: SystemRoleInfoVO) {
            const params = {roleId: row.roleId};
            const result = await RequestService.post<Array<SystemRoleInfoVO>>('delete-system-role-info', params);
            if (result.isError) {
                throw new BizError(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        }
    }
});